<?php namespace web\home\controller;


/**
 * Created by PhpStorm.
 * User: qianchenmiao
 * Date: 2016/10/17
 * Time: 1:00
 */
class MyCar extends  Common{
	private $db;
	public function __construct(){
		parent::__construct();
		$this->db=new \system\model\Address();
	}
//购物车index
	public function index(){
		if($_SESSION['cart']){
			foreach ($_SESSION['cart'] as$k=>$v){
				$goodData[$k]=$v;
			}
			foreach($goodData['goods'] as $k=>$v){
				if($v){
					$goodsData['goods'][$k]=$v;
					$goodsData['goods'][$k]['pic']=Db::table('goods')->where('gid',$v['id'])->pluck('pic');
				}

			}
		}
		View::with('goodsData',$goodsData)->make();
	}
//	异步修改购物车
	public function ajaxEdit(){
		$data=$_POST;
		$data=[
			'sid'=>$data['sid'],// 唯一 sid，添加购物车时自动生成
			'num'=>$data['num']
		];
		Cart::update($data);
	}
	//异步删除购物车
	public function ajaxDel(){

		if(isset($_POST['sid'])){
			$sid=$_POST['sid'];
		};
		if(isset($_POST['sids'])){
			$str=$_POST['sids'];
			$str=rtrim("$str",',');
			$arr=explode(",",$str);
			foreach($arr as $v){
				unset($_SESSION['cart']['goods'][$v]);
			}
		};
		unset($_SESSION['cart']['goods'][$sid]);
		echo 1;

	}
	//异步判断商品是否选中choose为1是选中为2是没选中
	public function ajaxChoose(){
		$data=$_POST;
		$sid=$data['sid'];
		$choose=$data['choose'];
		if($choose==1&&$sid){

			foreach($_SESSION['cart']['goods'] as $k=>$v){
				$hooses=$_SESSION['cart']['goods'][$k]['choose'];
//				判断是否是二次点击
				if($hooses==1){
					$_SESSION['cart']['goods'][$k]['choose']=2;
				}
			}
			$_SESSION['cart']['goods'][$sid]['choose']=$choose;
		}elseif($choose==3){

			foreach($_SESSION['cart']['goods'] as $k=>$v){
				$_SESSION['cart']['goods'][$k]['choose']=1;
			}
			echo 1;

		}else{
			$_SESSION['cart']['goods'][$sid]['choose']=$choose;
		}
	}
	//订单核对页
	public function checkOrder(){
		$goodsData=$_SESSION['cart']['goods'];
		$anyTotal=0;
		foreach($goodsData as $k=>$v){
			if($v['choose']==1){
				$data['goods'][$k]=$v;
				$anyTotal+=$v[total];
//				获取选中的订单以后分配到页面
				$data['anyTotal']=$anyTotal;
			}
		}

		//添加地址
		if(IS_POST){
			if($this->db->store()){
				message('地址添加成功',u('checkOrder'),'success');
			}else{
				message($this->db->getError(),'back','success');
			}
		}
			//		获取地址数据

		$huid=$_SESSION['homeUser']['uid'];
		$addressData=$this->db->where('huid',$huid)->get();
		View::with('addressData',$addressData );
		View::with('data',$data)->make();
	}
//删除地址
public function delress(){
	$rid=q('get.rid',0,'intval');
	$huid=$_SESSION['homeUser']['uid'];
	$this->db->where('rid',$rid)->where('huid',$huid)->delete();
	message('删除成功',u("checkOrder"),'success');
}
//订单递交异步存入数据库
public function ajaxStore(){
	if($_POST['rid']){$rid=$_POST['rid'];
	}
//	获取地址信息
	$address=Db::table('address')->where('rid',$rid)->first();
//	商品获取数据
	$goodsData=$_SESSION['cart']['goods'];
	$anyTotal=0;
	foreach($goodsData as $k=>$v){
		if($v['choose']==1){
			$data['goods'][$k]=$v;
			$anyTotal+=$v[total];
//				获取选中的订单以后放入模型类
			$data['anyTotal']=$anyTotal;
		}
	}
	$data['address']=$address;
	if(IS_POST){
		$db=new \system\model\Order();
		if($data['goods']){

			$oid=$db->store($data);
			foreach($goodsData as $k=>$v){
				if($v['choose']==1) {
					unset($_SESSION['cart']['goods'][$k]);
				}
			}

			echo json_encode(['check'=>1,'oid'=>$oid]);
		}else{

			echo json_encode(['check'=>2]);
		}
	}
	die;
}
//	订单递交成功页面
	public  function successOrder(){
		$oid=q('get.oid',0,'intval');
		if($oid){
			$orderData=Db::table('order')->where('oid',$oid)->first();
		}
		$d=date('Y-m-d H:i:s',$orderData['date']);
		$d=date('Y-m-d H:i:s',strtotime("$d   +10   hour"));
		$orderData['tomorrow']=$d;
		View::with('orderData',$orderData)->make();
	}
}